﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WisPortal
{

    public class AJAXNotification
    {
        public string extra { get; set; }
        public string message { get; set; }
        public string message_state { get; set; }
        public string title { get; set; }
        public string type { get; set; }
        public bool @new { get; set; }
    }


    public class AJAXResult
    {
        private const string NotificationTitle = "Notice";

        public string force_redirection { get; set; }
        public Dictionary<string, string> html { get; set; }
        public Dictionary<string,AJAXNotification> notifications{get;set;}

        public AJAXResult(string rev, string content, string title, string msg, string extra, NotificationType notificationType)
        {
            html = new Dictionary<string, string>();
            html.Add(rev, content);
            if (!string.IsNullOrEmpty(msg))
                InitNotifications(title, msg, extra, notificationType);
        }

        public AJAXResult(string msg, NotificationType notificationType)
        {
            InitNotifications(UI.Localize(NotificationTitle), msg, "", notificationType);
        }

        /// <summary>
        /// Return notice object
        /// </summary>
        /// <param name="title"></param>
        /// <param name="msg"></param>
        /// <param name="extra"></param>
        /// <param name="notificationType"></param>
        public AJAXResult(string title,string msg,string extra, NotificationType notificationType)
        {
            InitNotifications(title, msg, extra, notificationType);
        }

        private void InitNotifications(string title, string msg, string extra, NotificationType notificationType)
        {
            notifications = CreateNotification(title, msg, extra, notificationType);            
        }

        /// <summary>
        /// Generate a notification object
        /// </summary>
        /// <param name="title"></param>
        /// <param name="msg"></param>
        /// <param name="extra"></param>
        /// <param name="notificationType"></param>
        /// <returns></returns>
        public static Dictionary<string,AJAXNotification> CreateNotification(string title, string msg, string extra, NotificationType notificationType)
        {
            AJAXNotification n = new AJAXNotification();
            n.@new = true;
            n.message_state = "I";
            n.message = UI.Localize(msg);
            n.extra = UI.Localize(extra);
            n.title = UI.Localize(title);
            if (notificationType == NotificationType.Information)
                n.type = "N";
            else if (notificationType == NotificationType.Alert)
                n.type = "W";
            else if (notificationType == NotificationType.Error)
                n.type = "E";
            else
                n.type = "N";
            string id = Guid.NewGuid().ToString("N");
            Dictionary<string, AJAXNotification> ns = new Dictionary<string, AJAXNotification>();
            ns.Add(id, n);
            return ns;
        }

        public AJAXResult()
        { 
        }

    }
}
